<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {
	function __construct() {
		parent::__construct();
		session_start();
		
		/*
		if (url_contains(ADMIN_FOLDER . '/admin')) {
			if (!empty($_SESSION) && $_SESSION['ucid'] != ADMIN_UCID) { show_404(); }
		}*/
		
		// 管理员测试
		//$_SESSION['ucid'] = ADMIN_UCID;//ADMIN_UCID
	}
	
	function _ajax_validate($validate_data) {
		if (isset($validate_data['username'])) {
			$username = $validate_data['username'];
			if ($username == '') {
				$this->output->set_output('username|null|warning');
				return FALSE;
			} elseif (mb_strlen($username) > 16 OR mb_strlen($username) < 5) {
				$this->output->set_output('username|length|warning');
				return FALSE;
			} elseif (!filter_var($username, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z][a-zA-Z0-9_]+$/")))) {
				$this->output->set_output('username|pattern|warning');
				return FALSE;
			}
		}
	
		if (isset($validate_data['password'])) {
			$password = $validate_data['password'];
			if ($password == '') {
				$this->output->set_output('password|null|warning');
				return FALSE;
			} elseif (mb_strlen($password) > 10 OR mb_strlen($password) < 4) {
				$this->output->set_output('password|length|warning');
				return FALSE;
			}
		}
		
		return TRUE;
	}
	
	/**
	 * 得到表单数据
	 * @param $unset 过滤数据 可以是字符串 也可以是数组
	 */
	function _get_form_array($form_fields_data, $unset = '') {
		foreach ($form_fields_data as $v) {
			if (is_array($unset)) {
				if (!in_array($v['name'], $unset)) {
					$form_data[$v['name']] =  $v['value'];
				}
			} else {
				if ($v['name'] != $unset) {
					$form_data[$v['name']] =  $v['value'];
				}
			}
		}
		return $form_data;
	}
}